
// TextEditView.cpp : implementation of the CTextEditView class
//

#include "stdafx.h"
// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "TextEdit.h"
#endif

#include "TextEditDoc.h"
#include "TextEditView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CTextEditView

IMPLEMENT_DYNCREATE(CTextEditView, CFormView)

BEGIN_MESSAGE_MAP(CTextEditView, CFormView)
	ON_WM_CONTEXTMENU()
	ON_WM_RBUTTONUP()
	ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
	ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
	ON_COMMAND(ID_EDIT_CUT, OnEditCut)
	ON_WM_DROPFILES()
	ON_WM_SIZE()
END_MESSAGE_MAP()

// CTextEditView construction/destruction

CTextEditView::CTextEditView()
	: CFormView(CTextEditView::IDD)
	, m_valEditor(_T(""))
{
	// TODO: add construction code here

}

void CTextEditView::OnEditPaste()
{
	//m_ctrlEditor.SetSel(0, -1);
	m_ctrlEditor.Paste();
}

void CTextEditView::OnEditCopy()
{
	m_ctrlEditor.Copy();
}

void CTextEditView::OnEditCut()
{
	m_ctrlEditor.Cut();
}

CTextEditView::~CTextEditView()
{
}

void CTextEditView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_RICHEDITEDITOR, m_ctrlEditor);
	DDX_Text(pDX, IDC_RICHEDITEDITOR, m_valEditor);
}

BOOL CTextEditView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CTextEditView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

	CFile cFile(TEXT("TextEditView.cpp"), CFile::modeRead);
	EDITSTREAM es;
	es.dwCookie = (DWORD) &cFile;
	es.pfnCallback = MyStreamInCallback;
	m_ctrlEditor.StreamIn(SF_TEXT, es);
	m_ctrlEditor.SetBackgroundColor(FALSE, RGB(255 , 255, 255));
	//m_ctrlEditor.SetSel(-1, 0);

	//set the locations and sizes of the controls
	RECT r;
	this->GetClientRect(&r);
	SetSize(r.right - r.left,r.bottom - r.top);// sends the width and height of the dlg box

	//CRichEditView::OnInitialUpdate();
	CHARFORMAT cfm; 
	cfm.cbSize = sizeof(CHARFORMAT); 
	cfm.dwMask = CFM_FACE | CFM_SIZE | CFM_BOLD |
		CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_PROTECTED | CFM_COLOR; 
	cfm.dwEffects = CFE_BOLD | CFE_ITALIC; 
	cfm.yHeight = 240;
	cfm.crTextColor = RGB(0, 200, 0);
	cfm.dwEffects &= ~CFE_AUTOCOLOR;
	::lstrcpy(cfm.szFaceName, L"Times New Roman");

	m_ctrlEditor.SetDefaultCharFormat(cfm); 
	//m_ctrlEditor.SetSelectionCharFormat(cfm);
	//m_ctrlEditor.Invalidate();

	//SetMargins(CRect(720, 720, 720, 720));
}

void CTextEditView::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
	ClientToScreen(&point);
	OnContextMenu(this, point);
}

void CTextEditView::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
	theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}


// CTextEditView diagnostics

#ifdef _DEBUG
void CTextEditView::AssertValid() const
{
	CFormView::AssertValid();
}

void CTextEditView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CTextEditDoc* CTextEditView::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTextEditDoc)));
	return (CTextEditDoc*)m_pDocument;
}

DWORD CALLBACK CTextEditView::MyStreamInCallback( DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb )
{
	CFile *pFile = (CFile*) dwCookie;
	*pcb = pFile->Read(pbBuff, cb);
	return 0;
}

void CTextEditView::OnSize( UINT nType, int cx, int cy )
{
	CFormView::OnSize(nType, cx, cy);
	if(m_ctrlEditor.GetSafeHwnd()) SetSize(cx,cy);
}

void CTextEditView::OnDropFiles( HDROP hDropInfo )
{
	CString FileName;

	::DragQueryFile(hDropInfo, 0, FileName.GetBufferSetLength
		(_MAX_PATH), _MAX_PATH);

	FileName.ReleaseBuffer();
	::DragFinish(hDropInfo);

	//m_strPathname = FileName;
	//SetWindowTitle();
	//ReadFile();

	CFile cFile(FileName, CFile::modeRead);
	EDITSTREAM es;
	es.dwCookie = (DWORD) &cFile;
	es.pfnCallback = MyStreamInCallback;
	m_ctrlEditor.StreamIn(SF_TEXT, es);
	//m_ctrlEditor.SetBackgroundColor(FALSE, RGB(255,0, 0));
	m_ctrlEditor.SetSel(-1, 0);
}

#endif //_DEBUG


// CTextEditView message handlers



void CTextEditView::OnSetSelectionCharFormat()
{
	CHARFORMAT cf;

	cf.dwMask = CFM_COLOR;
	cf.crTextColor = RGB(0,0,255);
	m_ctrlEditor.SetSelectionCharFormat(cf);
	// color not work?????

	cf.dwMask = CFM_STRIKEOUT;
	cf.dwEffects = CFE_STRIKEOUT;
	m_ctrlEditor.SetSelectionCharFormat(cf);
	// still not work???

	cf.dwMask = CFM_STRIKEOUT|CFM_BOLD;
	cf.dwEffects = CFE_BOLD;
	m_ctrlEditor.SetSelectionCharFormat(cf);
	// work, but the normal char is also affected?????
}

void CTextEditView::SetSize( int cx, int cy )
{
	RECT r;

	//set coordinates for rich edit box
	r.left = 10; 
	r.top = 10;
	r.right = cx - 10;
	r.bottom = cy - 10;
	m_ctrlEditor.MoveWindow(&r);

	////set coordinates for Cancel button
	//r.left = cx - XY_BORDER - XY_BUTTONWIDTH; 
	//r.top = cy - XY_BOTTOM + XY_GAP;
	//r.right = cx - XY_BORDER;
	//r.bottom = cy - XY_BORDER;
	//m_cCancel.MoveWindow(&r);

	////set coordinates for Format button
	//r.left = cx - XY_BORDER - (3 * XY_BUTTONWIDTH) - (2 * XY_GAP); 
	//r.top = cy - XY_BOTTOM + XY_GAP;
	//r.right = cx - XY_BORDER - (2 * XY_BUTTONWIDTH) - (2 * XY_GAP);
	//r.bottom = cy - XY_BORDER;
	//m_cFormat.MoveWindow(&r);

	////set coordinates for Comments button
	//r.left = cx - XY_BORDER - (4 * XY_BUTTONWIDTH) - (3 * XY_GAP); 
	//r.top = cy - XY_BOTTOM + XY_GAP;
	//r.right = cx - XY_BORDER - (3 * XY_BUTTONWIDTH) - (3 * XY_GAP);
	//r.bottom = cy - XY_BORDER;
	//m_cComment.MoveWindow(&r);

	////set coordinates for Keywords button
	//r.left = cx - XY_BORDER - (5 * XY_BUTTONWIDTH) - (4 * XY_GAP); 
	//r.top = cy - XY_BOTTOM + XY_GAP;
	//r.right = cx - XY_BORDER - (4 * XY_BUTTONWIDTH) - (4 * XY_GAP);
	//r.bottom = cy - XY_BORDER;
	//m_cKeyword.MoveWindow(&r);

	////set coordinates for Compiler directives button
	//r.left = cx - XY_BORDER - (6 * XY_BUTTONWIDTH) - (5 * XY_GAP); 
	//r.top = cy - XY_BOTTOM + XY_GAP;
	//r.right = cx - XY_BORDER - (5 * XY_BUTTONWIDTH) - (5 * XY_GAP);
	//r.bottom = cy - XY_BORDER;
	//m_cDirective.MoveWindow(&r);
}


